package com.feiyu.buyer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feiyu.buyer.dao.BuyerDao;
import com.feiyu.buyer.dao.UserDao;
import com.feiyu.buyer.dto.selectBuyerDto;
import com.feiyu.buyer.dto.updateBuyerDto;
import com.feiyu.buyer.entity.BuyerEntity;
import com.feiyu.buyer.entity.UserEntity;
import com.feiyu.buyer.service.BuyerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Doate
 * @Date: Created in 2021/4/16 3:35
 * @Description:
 */

@Service
public class BuyerServiceImpl implements BuyerService {

    @Resource
    BuyerDao buyerDao;

    @Resource
    UserDao userDao;

    @Override
    public Map<String, Object> getInfo(int userId) {

        Map<String, Object> data = new HashMap<>();

        QueryWrapper<BuyerEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);

        BuyerEntity entity = buyerDao.selectOne(wrapper);

        QueryWrapper<UserEntity> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("user_id",userId);

        UserEntity userEntity = userDao.selectOne(wrapper1);

        BeanUtil.copyProperties(entity,data);
        BeanUtil.copyProperties(userEntity,data);


        return data;
    }

    @Override
    public boolean updateInfo(updateBuyerDto dto) {

        BuyerEntity buyerEntity = new BuyerEntity();
        UserEntity userEntity = new UserEntity();

        buyerEntity.setBuyerAvator(dto.getBuyerAvator());
        buyerEntity.setBuyerName(dto.getNickName());

        userEntity.setUserBirth(dto.getUserBirth());
        userEntity.setUserPwd(dto.getUserPwd());
        userEntity.setUserTel(dto.getUserTel());

        UpdateWrapper<BuyerEntity> wrapperBuyer = new UpdateWrapper<>();
        wrapperBuyer.eq("user_id",dto.getUserId());
        buyerDao.update(buyerEntity,wrapperBuyer);

        UpdateWrapper<UserEntity> wrapperUser = new UpdateWrapper<>();
        wrapperUser.eq("user_id",dto.getUserId());
        userDao.update(userEntity,wrapperUser);

        return true;
    }

    @Override
    public Map<String, Object> searchByParam(selectBuyerDto dto) {

        QueryWrapper<BuyerEntity> wrapper = new QueryWrapper<>();
        if (dto.getDelFlag()!=null) {
            wrapper.eq("del_flag",dto.getDelFlag());
        }

        IPage<BuyerEntity> iPage = buyerDao.selectPage(new Page<BuyerEntity>(dto.getPage(), dto.getLimit()), wrapper);
        List<BuyerEntity> records = iPage.getRecords();

        Map<String, Object> data = new HashMap<>();
        data.put("totalCount", iPage.getTotal());
        data.put("pageSize", iPage.getSize());
        data.put("totalPage", iPage.getPages());
        data.put("currPage", iPage.getCurrent());

        data.put("list",records);

        return data;
    }

    @Override
    public BuyerEntity searchById(int buyerId) {

        QueryWrapper<BuyerEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("buyer_id",buyerId);

        BuyerEntity entity = buyerDao.selectOne(wrapper);

        return entity;
    }
}
